package com.columboid.testharness.gui;

import javax.swing.*;
import javax.swing.JCheckBox;

import com.columboid.testharness.gui.swtdesigner.SwingResourceManager;
import com.columboid.testharness.main.Login;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class LoginFrame extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public LoginFrame() {

		jPanelInit();

		loginButton = new JButton();
		loginButton.setText("Login");
		loginButton.setBounds(35, 184, 94, 28);
		getContentPane().add(loginButton);

		cancelButton = new JButton();
		cancelButton.setText("Cancel");
		cancelButton.setBounds(135, 184, 96, 28);
		getContentPane().add(cancelButton);

		optionButton = new JButton();
		optionButton.setText("Option >>");
		optionButton.setBounds(237, 184, 94, 28);
		getContentPane().add(optionButton);

		loginLable = new JLabel();
		loginLable.setText("Please enter your user name and password:");
		loginLable.setBounds(new Rectangle(57, 22, 257, 22));
		contentPanel.add(loginLable);

		usernameLabel = new JLabel();
		usernameLabel.setText("Username");
		usernameLabel.setBounds(10, 73, 66, 18);
		getContentPane().add(usernameLabel);

		passwordLabel = new JLabel();
		passwordLabel.setText("Password");
		passwordLabel.setBounds(10, 111, 66, 18);
		getContentPane().add(passwordLabel);

		serverLabel = new JLabel();
		serverLabel.setText("Server");
		serverLabel.setBounds(10, 148, 66, 18);
		getContentPane().add(serverLabel);

		serverField = new JTextField();
		serverField.setText(DefaultURL);
		serverField.setToolTipText("Please enter Server address");
		serverField.setBounds(82, 146, 104, 22);
		getContentPane().add(serverField);

		challengeCheckBox = new JCheckBox();
		challengeCheckBox.setText("Challenge");
		challengeCheckBox.setBounds(207, 144, 94, 22);
		getContentPane().add(challengeCheckBox);

		usernameField = new JTextField();
		usernameField.setBounds(82, 71, 205, 22);
		usernameField.setToolTipText("Please enter your username");
		getContentPane().add(usernameField);

		passwordField = new JPasswordField();
		passwordField.setBounds(82, 109, 205, 22);
		passwordField.setToolTipText("Please enter your password");
		getContentPane().add(passwordField);

		final JLabel label = new JLabel();
		label.setIcon(SwingResourceManager.getIcon(LoginFrame.class,
				"icons/loginkey.gif"));
		label.setBounds(10, 12, 41, 42);
		getContentPane().add(label);

		loginButton.addActionListener(this);
		cancelButton.addActionListener(this);

		usernameField.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int k = e.getKeyCode();
				if (k == KeyEvent.VK_ENTER) {
					loginButton.doClick();
				}
			}
		});

		passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int k = e.getKeyCode();
				if (k == KeyEvent.VK_ENTER) {
					loginButton.doClick();
				}
			}
		});

		optionButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				if (!OpenAddr) {
					showAddr();
				} else {
					hideAddr();
				}

			}
		});

		hideAddr();

	}

	private void jPanelInit() {
		contentPanel = (JPanel) getContentPane();
		contentPanel.setLayout(null);
		setResizable(false);
		// setSize(new Dimension(362, 284));
		setTitle("Columboid Login");
		setIconImage(SwingResourceManager.getImage(LoginFrame.class,
				"icons/clogo.gif"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private void hideAddr() {

		setSize(new Dimension(362, 254));
		serverField.setVisible(false);
		serverLabel.setVisible(false);
		challengeCheckBox.setVisible(false);
		loginButton.setBounds(35, 154, 94, 28);
		cancelButton.setBounds(135, 154, 96, 28);
		optionButton.setBounds(237, 154, 94, 28);
		optionButton.setText("Option >>");
		OpenAddr = false;

	}

	private void showAddr() {

		setSize(new Dimension(362, 284));
		serverField.setVisible(true);
		serverLabel.setVisible(true);
		challengeCheckBox.setVisible(true);
		loginButton.setBounds(35, 184, 94, 28);
		cancelButton.setBounds(135, 184, 96, 28);
		optionButton.setBounds(237, 184, 94, 28);
		optionButton.setText("Option <<");
		OpenAddr = true;
	}

	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd.equals("Login")) {
			if (usernameField.getText().equals("")) {
				JOptionPane.showMessageDialog(this,
						"Please enter your username!");
				usernameField.requestFocus();
				return;
			}

			if (passwordField.getPassword() == null
					|| passwordField.getPassword().length == 0)

			{
				JOptionPane.showMessageDialog(this,
						"Please enter your password!");
				passwordField.requestFocus();
				return;
			}

			if (serverField.getText().equals("")) {
				JOptionPane.showMessageDialog(this,
						"Please enter server IP address!");
				serverField.requestFocus();
				return;
			}
			Login lg = new Login();
			boolean re = false;
			if (challengeCheckBox.isSelected())
				re = lg.userLoginChallenge(usernameField.getText(), new String(
						passwordField.getPassword()), serverField.getText());
			else
				re = lg.userLogin(usernameField.getText(), new String(
						passwordField.getPassword()), serverField.getText());
			if (re) {
				JOptionPane.showMessageDialog(this, "Login Sucess!");
			} else {
				JOptionPane.showMessageDialog(this, "Login Failed!");
			}
		} else if (cmd.equals("Cancel")) {
			System.exit(0);
		}
	}

	private JPanel contentPanel;
	private JTextField usernameField;
	private JPasswordField passwordField;
	private JTextField serverField;
	private JButton optionButton;
	private JButton loginButton;
	private JButton cancelButton;
	private JLabel loginLable;
	private JLabel usernameLabel;
	private JLabel passwordLabel;
	private JLabel serverLabel;
	private JCheckBox challengeCheckBox;
	private boolean OpenAddr = false;
	private final String DefaultURL = "69.172.67.109";
}
